#include <stdio.h>

long long int FACT(int n)
{
	long long int sum = 1;
	for (int i = 2; i <= n; i++) sum *= i;
	return sum;
}

int get_digit(int a, int* res)
{
	int cnt = 0;
	while (a > 0)
	{
		*res = a % 10;
		a /= 10;
		res++;
		cnt++;
	}
	return cnt;
}

int num_reverse(int a)
{
	int data[15] = { 0 }, output = 0, t = 1;
	int n = get_digit(a, data);
	n--;
	for (int i = n; i >= 0; i--)
	{
		output = output + (data[i] * t);
		t *= 10;
	}
	return output;
}

int if_prime(int n)
{
	if (n <= 1) return 0;
	for (int i = 2; i < n; i++)
	{
		if (n % i == 0) return 0;
	}
	return 1;
}

void pefect_num(int a)
{
	int factors_sum = 0, factors[9999] = { 0 }, k = 0;
	for (int i = 1; i <= a / 2; i++)
	{
		if (a % i == 0)
		{
			factors[k++] = i;
			factors_sum += i;
		}
	}
	k--;
	if (factors_sum == a)
	{
		printf("%d is a Pefect Number. It's factors are ", a);
		for (int i = 0; i < k; i++)
		{
			printf("%d, ", factors[i]);
		}
		printf("%d. \n", factors[k]);
		return;
	}
	else {
		//printf("%d is not a Pefect Number. \n", a);
		return;
	}
}

int if_7num(int n)
{
	int t[18] = { 0 };
	int k = get_digit(n, t);
	if (n % 7 == 0) return 1;
	for (int j = 0; j < k; j++)
	{
		if (t[j] == 7) return 1;
	}
	return 0;
}

void swap(int* a, int* b) 
{
	int t = *a;
	*a = *b;
	*b = t;
}

int gcd(int a, int b)
{
	while (b != 0)
	{
		a = a % b;
		swap(a, b);
	}
	return a;
}

int lcm(int a, int b)
{
	return a * b / gcd(a, b);
}

int main()
{
	int m, n;
	printf("Please input m and n: \n");
	scanf("%d%d", &m, &n);
    
    printf("All \"7num\" between %d and %d: \n",m,n);
	for (int i = m; i <= n; i++)
	{
		if (if_7num(i)) printf("%d ", i);
	}
	printf("\n\n");

	if(m<21) printf("m! = %I64d \n",FACT(m));
	else printf("m! is too big!\n"); 
	if(n<21) printf("n! = %I64d \n",FACT(n));
	else printf("n! is too big!\n"); 
	printf("\n");

    printf("All palindromic number between %d and %d: \n",m,n);
	for (int i = m; i <= n; i++)
	{
		if (i == num_reverse(i)) printf("%d ", i);
	}
	printf("\n\n");

	printf("The greatest common divisor is %d; \n", gcd(m, n));
	printf("The lesat common multiple is %d. \n", lcm(m,n));
	printf("\n");

    printf("All prime number between %d and %d: \n",m,n);
	for (int i = m; i <= n; i++)
	{
		if (if_prime(i)) printf("%d ", i);
	}
	printf("\n\n");

    printf("All perfect number between %d and %d: \n",m,n);
	for (int i = m; i <= n; i++)
	{
		pefect_num(i);
	}
	printf("\n");
	return 0;
}
